<?php
 
/**
 * @author  wangl  2016-5-16
 * @notice  提高程序运行效率 代码中“ ->' 双引号改为单引号 ; php.ini 需要开放 extension=php_curl.dll
 */

class Webpost{
		private  $cloudUrl = "http://120.25.92.158:8088/k3cloud/";  //K/3 Cloud 业务站点地址
		//登陆参数
		private $data = array(
				'57674cbede020c',
				'user',            	//用户名
				'123.com',
				2052,            	//语言标识
		);
		public function invoke_post($url,$post_content,$cookie_jar,$isLogin)
		{
			$ch = curl_init($url);
			$this_header = array(
					'Content-Type: application/json',
					'Content-Length: '.strlen($post_content)
			);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
			curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			if($isLogin)
			{
				curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
			}
			else
			{
				curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
			}
			curl_setopt($ch, CURLOPT_TIMEOUT, 30);
			$result = curl_exec($ch);
			curl_close($ch);
			return $result;
		}
		
		//构造Web API请求格式
		public function create_postdata($args) {
			$postdata = array(
					'format'	=> 1,
					'useragent'	=> 'ApiClient',
					'rid'		=> $this->create_guid(),
					'parameters'=> $args,
					'timestamp'	=> date('Y-m-d'),
					'v'			=> '1.0'
			);
			return json_encode($postdata);
		}
		
		//生成guid
		private function create_guid() {
			$charid = strtoupper(md5(uniqid(mt_rand(), true)));
			$hyphen = chr(45);
			$uuid 	= chr(123)
			.substr($charid, 0, 8).$hyphen
			.substr($charid, 8, 4).$hyphen
			.substr($charid,12, 4).$hyphen
			.substr($charid,16, 4).$hyphen
			.substr($charid,20,12)
			.chr(125);
			return $uuid;
		}
		
		//计算相关价格
		public function BillAmount($taxprice,$diccount,$tax,$nums) { 
			//含税单价$taxprice  折扣率% $diccount  税率% $tax  数量 $nums
			$tax							= $tax/100;
			$diccount						= $diccount/100;
			$billamount['price'] 			= $taxprice/(1+$tax);
			$billamount['FENTRYTAXAMOUNT'] 	= round(($nums*$taxprice-round($nums*$diccount*$taxprice,2))*$tax/(1+$tax),2);
			$billamount['FDiscount'] 		= round($nums*$taxprice*$diccount,2);
			$billamount['FAmount'] 			= round($nums*($taxprice/(1+$tax))-(round($nums*$taxprice*$diccount,2)/(1+$tax)),2);
			$billamount['FALLAMOUNT']		= round($nums*$taxprice-round($nums*$taxprice*$diccount,2),2);
			return $billamount;
		}
		
		
		//销售财务订单价格计算
		public function BillPrice($taxprice,$discount_amount,$tax,$nums){
			//含税单价$taxprice  折扣额 $discount_amount  税率% $tax  数量 $nums
			$tax							= $tax/100;
			$diccount						= $discount_amount/($nums*$taxprice);
			$billamount['price'] 			= $taxprice/(1+$tax);
			$billamount['tax_amount'] 		= round(($nums*$taxprice-round($nums*$diccount*$taxprice,2))*$tax/(1+$tax),2);
			$billamount['FAmount'] 			= round($nums*($taxprice/(1+$tax))-(round($nums*$taxprice*$diccount,2)/(1+$tax)),2);
			$billamount['FALLAMOUNT']		= round($nums*$taxprice-round($nums*$taxprice*$diccount,2),2);
			return $billamount;	
		}
		
	
		//登陆
		public function invoke_login($cookie_jar)
		{
			$post_content = $this->create_postdata($this->data);
			$loginurl = $this->cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc';
			return $this->invoke_post($loginurl,$post_content,$cookie_jar,TRUE);
		}
		
		//查询
		public function invoke_search($post_content,$cookie_jar)
		{
			$invokeurl = $this->cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc';
			return $this->invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
		}
		
		//查询订单
		public function order_search($post_content,$cookie_jar)
		{
			$invokeurl = $this->cloudUrl.'K3ServiceExtend.QueryService.QueryOrder,K3ServiceExtend.common.kdsvc';
			return $this->invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
		}
		
		//查询退货申请
		public function out_order_search($post_content,$cookie_jar)
		{
			$invokeurl = $this->cloudUrl.'K3ServiceExtend.QueryService.QueryMRApp,K3ServiceExtend.common.kdsvc';
			return $this->invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
		}
		
		//查询数据列表
		public function invoke_searchlist($post_content,$cookie_jar)
		{
			$invokeurl = $this->cloudUrl.'K3ServiceExtend.QueryService.Query,K3ServiceExtend.common.kdsvc';
			return $this->invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
		}
		
		//保存
		public function invoke_save($post_content,$cookie_jar)
		{
			$invokeurl = $this->cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc';
			return $this->invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
		}
		
		//提交
		public function invoke_submit($post_content,$cookie_jar)
		{
			$invokeurl = $this->cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService. Submit.common.kdsvc';
			return $this->invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
		}
		
		//审核
		public function invoke_audit($post_content,$cookie_jar)
		{
			$invokeurl = $this->cloudUrl.'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc';
			return $this->invoke_post($invokeurl,$post_content,$cookie_jar,FALSE);
		}

		/*
		 * 添加物料
		 * $product_item_no 商品编码
		 * $productId 商品id
		 * $product_name 商品名字
		 * $product_manufacturer 商品生产厂商
		 * $product_brand  品牌
		 * $FMaterialGroup  商品分组
		 */
		public function add_BD_MATERIAL($product_item_no,$product_name,$product_manufacturer,$product_brand,$FMaterialGroup){
			$cookie_jar = tempnam('./tmp','CloudSession');
			$result 	= $this->invoke_login($cookie_jar);
			$result 	= json_decode($result,true);
			header("Content-type: text/html; charset=utf-8");
			if($result['LoginResultType'] == 1 || $result['LoginResultType'] == -5)
			{
				$model_MATERIAL = '{
					"FMATERIALID":"0",
					"FCreateOrgId":{"FNumber":"0"},
					"FUseOrgId":{"FNumber":"0"},
					"FNumber":"'.$product_item_no.'",
					"FName":"'.$product_name.'",
					"F_SCCJ":"'.$product_manufacturer.'",
					"F_PP":"'.$product_brand.'",
					"FMaterialGroup": {"FNumber": "'.$FMaterialGroup.'"},
					"SubHeadEntity": {
					    "FCategoryID":{"FNumber":"CHLB01_SYS"},
						"FBaseUnitId":{"FNumber":"Pcs"},
					},
					}';
					
				$data_model = '{"Creator":"String","NeedUpDateFields":[],"Model":'.$model_MATERIAL.'}';
				$data 		= array(
						'BD_MATERIAL',
						$data_model,
				);
				$post_content 	 = $this->create_postdata($data);
				$result_MATERIAL = $this->invoke_save($post_content,$cookie_jar);
				$result_MATERIAL = json_decode($result_MATERIAL);
				if($result_MATERIAL->Result->ResponseStatus->IsSuccess)
				{
					return 1;
				}
				else
				{
					return 0;
				}
				
			}
			else
			{
				return 0;
			}
		}
		
		/*
		 * 查看物料是否存在
		 * $product_item_no 商品编码
		 * 
		*/
		public function check_MATERIAL($product_item_no){
			$cookie_jar = tempnam('./tmp','CloudSession');
			$result 	= $this->invoke_login($cookie_jar);
			$result 	= json_decode($result,true);
			header("Content-type: text/html; charset=utf-8");
			if($result['LoginResultType'] == 1 || $result['LoginResultType'] == -5)
			{
				$where = "FNumber='".$product_item_no."'";
				$data_num = array(
        		'0'=>array(
        			"FormId"       => 'BD_MATERIAL',  	//业务对象标识FormId
        			"TopRowCount"  => 0,               			//数字，返回数据的最大条数，0表示不限制
        			"FilterString" => "$where" ,    //字符串，过滤条件。
        			"OrderString"  => "" ,     					//字符串，排序字符串
        			"FieldKeys"    => "FNumber,FName" , 		//字符串，需要返回的字段列表，字段自己爱你用逗号分隔。	对于引用对象中的字段名，使用：字段名 + ”.” + 引用对象中的字段名，例如 材料的编号字段，可以使用FMaterialId.FNumber 作为字段名。
        			)
	        	);
	        	$post_content_num 	= $this->create_postdata($data_num);
	        	$website_nums 		= $this->invoke_searchlist($post_content_num,$cookie_jar);
	        	$website_nums 		= json_decode($website_nums);
	        	$website_nums_all 	= $website_nums->Result->Result;
	        	if(count($website_nums_all)>0) 
	        	{
	        		return 1;
	        	}
	        	else
	        	{
	        		return 0;
	        	}
			}
			else
			{
				return 0;
			}
		}
		
		/*
		 * 销售出库单  SAL_OUTSTOCK
		 * 传入:$websiteId站点ID；$productid物料ID；$increment_id订单号；$qty_ordered数量；$taxprice含税价格；$discount_amount折扣额；$tax税率
		 * 客户id固定2000000 --- FCustomerID ; 
		 * 是否赠品---FIsFree ;
		 */
		public function add_SAL_OUTSTOCK($websiteId,$product_item_no,$increment_id,$qty_ordered,$taxprice,$discount_amount,$tax){
			$billamount        = $this->BillPrice($taxprice,$discount_amount,$tax,$qty_ordered);
			$FPRICE_F1		   = $billamount['price'];
			$FAmount  		   = $billamount['FAmount'];
			$FENTRYTAXAMOUNT   = $billamount['tax_amount'];
			$FALLAMOUNT		   = $billamount['FALLAMOUNT'];
			if($FALLAMOUNT==0 || empty($FALLAMOUNT))
			{
				$FIsFree = 'true';
			}
			else
			{
				$FIsFree = 'false';
			}
			if($this->check_MATERIAL($product_item_no))
			{
				$FEntity_string = '
		        		{
					      "FMaterialId": { "FNumber": "'.$product_item_no.'" },
					      "FPRICEBASEQTY": '.$qty_ordered.',
					      "FRealQty": '.$qty_ordered.',
					      "FPRICEUNITQTY": '.$qty_ordered.',
					      "FSALUNITQTY": '.$qty_ordered.',
					      "FSALBASEQTY": '.$qty_ordered.',
					      "FBASEUNITQTY": '.$qty_ordered.',
					      "FStockID": {"FNumber": "'.$websiteId.'"},
					      "FPrice": '.$FPRICE_F1.',
					      "FTaxPrice": '.$taxprice.',
					      "FAmount": '.$FAmount.',
					      "FEntryTaxRate":  '.$tax.',
					      "FEntryTaxAmount": '.$FENTRYTAXAMOUNT.',
					      "FAllAmount": '.$FALLAMOUNT.',
					      "FIsFree": "'.$FIsFree.'",
					      "FEntrynote": "'.$increment_id.'"
					    }';
				$cookie_jar 	= tempnam('./tmp','CloudSession');
				$result 		= $this->invoke_login($cookie_jar);
				header("Content-type: text/html; charset=utf-8");
				$FEntity_string = rtrim($FEntity_string, ",");
				$data 			= date('Y-m-d',time());
				$model_OUTSTOCK = '{
						  "FBillTypeID": {"FNumber": "XSCKD01_SYS"},
						  "FDate": "'.$data.'",
						  "FStockOrgId": {"FNumber": "'.$websiteId.'"},
						  "FSaleOrgId": {"FNumber": "'.$websiteId.'"},
						  "FCustomerID": {"FNumber": "2000000"},
						  "SubHeadEntity": {
						    		"FSettleCurrID": {"FNumber": "PRE001"},
						    		"FSettleOrgID": {"FNumber": "'.$websiteId.'"}
			    		  },
						  "FEntity": ['.$FEntity_string.']
					}';
				//echo $model_OUTSTOCK;exit;
				$data_model = '{"Creator":"String","NeedUpDateFields":[],"Model":'.$model_OUTSTOCK.'}';
				$data = array(
						'SAL_OUTSTOCK',
						$data_model,
				);
				$post_content  = $this->create_postdata($data);
				$result 	   = $this->invoke_save($post_content,$cookie_jar);
				$result 	   = json_decode($result);
				if($result->Result->ResponseStatus->IsSuccess)
				{
					return 1;
				}
				else
				{
					return 4;
				}	
			}else{
				return 3;
			}	
		}
		
		/*
		 * 其它入库操作STK_MISCELLANEOUS
		 * $websiteId,
		 * $FSUPPLIERID,
		 * $FACCEPTANCE,
		 * $FDEPTID,
		 * $FQTRK_TYPE  其他入库用途
		 * $productId,
		 * $qty
		 * 
		 */
		public function add_STK_MISCELLANEOUS($comment,$websiteId,$FSUPPLIERID,$FACCEPTANCE,$FDEPTID,$FQTRK_TYPE,$product_item_no,$qty){
			$cookie_jar = tempnam('./tmp','CloudSession');
			$result 	= $this->invoke_login($cookie_jar);
			$result 	= json_decode($result,true);
			$datetime 	= date('Y-m-d',time());
			if($this->check_MATERIAL($product_item_no))
			{
				$STK_MISCELLANEOUS = '{
						"FBillTypeID":{ "FNumber": "QTRKD01_SYS"},
						"FDate": "'.$datetime.'" ,
						"FStockOrgId":{"FNumber":"'.$websiteId.'"},
						"FSUPPLIERID": {"FNumber": "'.$FSUPPLIERID.'"},
						"FACCEPTANCE": {"FNumber": "'.$FACCEPTANCE.'"},
						"FDEPTID": {"FNumber": "'.$FDEPTID.'"},
						"FQTRK_TYPE": {"FNumber": "'.$FQTRK_TYPE.'"},
						"FEntity": [
						        {
						          "FMATERIALID": {"FNumber": "'.$product_item_no.'"},
						          "FQty": '.$qty.',
						          "FSTOCKID":{"FNumber": "'.$websiteId.'"},
						          "FEntryNote":"'.$comment.'",
						        }
						      ]
					}';
				$data_model = '{"Creator":"String","NeedUpDateFields":[],"Model":'.$STK_MISCELLANEOUS.'}';
				$k3data 	= array(
						'STK_MISCELLANEOUS',
						$data_model,
				);
				$post_content	= $this->create_postdata($k3data);
				$result  		=  $this->invoke_save($post_content,$cookie_jar);
				$result 		= json_decode($result);
				if($result->Result->ResponseStatus->IsSuccess)
				{
					return 1;
				}
				else
				{
					return 0;
				}			
			}
			else
			{
				return 0;
			}
		}
		
		/*
		 * 其它出库操作
		 * FPICKERID   领料人
		 * FSUPPLIERID 供应商
		 * FACCEPTANCE 验收员
		 */
		public function add_STK_MisDelivery($comment,$websiteId,$FSUPPLIERID,$FACCEPTANCE,$FDEPTID,$FQTRK_TYPE_out,$product_item_no,$nums){
			$cookie_jar = tempnam('./tmp','CloudSession');
			$result 	= $this->invoke_login($cookie_jar);
			$result 	= json_decode($result,true);
			$datetime 	= date('Y-m-d',time());
			if($this->check_MATERIAL($product_item_no))
			{
				$STK_MISCELLANEOUS = '{
						"FBillTypeID":{ "FNumber": "QTCKD01_SYS"},
						"FDate": "'.$datetime.'" ,
						"FStockOrgId":{"FNumber":"'.$websiteId.'"},
						"FSUPPLIERID": {"FNumber": "'.$FSUPPLIERID.'"},
						"FACCEPTANCE": {"FNumber": "'.$FACCEPTANCE.'"},
						"FDEPTID": {"FNumber": "'.$FDEPTID.'"},
						"FQTCK_TYPE": {"FNumber": "'.$FQTRK_TYPE_out.'"},
						"FPICKERID":{"FNumber": "'.$FACCEPTANCE.'"},
						"FEntity": [
						        {
						          "FMATERIALID": {"FNumber": "'.$product_item_no.'"},
						          "FQty": '.$nums.',
						          "FSTOCKID":{"FNumber": "'.$websiteId.'"},
						          "FEntryNote":"'.$comment.'",
						        }
						      ]
					}';
				//echo $STK_MISCELLANEOUS;exit;
				$data_model = '{"Creator":"String","NeedUpDateFields":[],"Model":'.$STK_MISCELLANEOUS.'}';
				$k3data 	= array(
						'STK_MisDelivery',
						$data_model,
				);
				$post_content = $this->create_postdata($k3data);
				$result  	  = $this->invoke_save($post_content,$cookie_jar);
				$result 	  = json_decode($result);
				if($result->Result->ResponseStatus->IsSuccess)
				{
					return 1;
				}
				else
				{
					return 0;
				}
			}
			else
			{
				return 0;
			}
		}
		
		/*
		 * 销售退货单  SAL_RETURNSTOCK
		 * 退货客户id -- FRetcustId   ; 
		 * 站点id ---FSaleOrgId,FStockId   ;
		 * 商品编码 ---FMaterialId ;
		 * 订单号   FNOTE ; 
		 * 是否赠品 FIsFree        
		 */
		public function add_SAL_RETURNSTOCK($websiteId,$orderId,$product_item_no,$qty,$taxprice,$FAmount){
			if($FAmount==0 || empty($FAmount))
			{
				$FIsFree = 'true';
			}
			else
			{
				$FIsFree = 'false';
			}
			if($this->check_MATERIAL($product_item_no))
			{
				$FEntity_string = '{
		                	"FNOTE":"'.$orderId.'",
		                	"FMaterialId": {"FNumber": "'.$product_item_no.'"},
		                	"FRealQty":'.$qty.',
		                	"FPRICEUNITQTY":'.$qty.',
		                	"FPRICEBASEQTY":'.$qty.',
		                	"FSALUNITQTY":'.$qty.',
		                	"FSALBASEQTY":'.$qty.',
		                	"FBASEUNITQTY":'.$qty.',
		                	"FStockId":{"FNumber": "'.$websiteId.'"},
		                	"FPrice": "'.$taxprice.'",
		                	"FTaxPrice": "'.$taxprice.'",
		                	"FAmount":  "'.$FAmount.'",
		                	"FEntryTaxRate":  "0",
		                	"FEntryTaxAmount":  "0",
		                	"FAllAmount":  "'.$FAmount.'",
		                	"FIsFree":"'.$FIsFree.'",
		    	}';
				$data 			= date('Y-m-d',time());
				$FEntity_string = rtrim($FEntity_string, ",");
				$webpost 		= new Webpost();
				$cookie_jar 	= tempnam('./tmp','CloudSession');
				$result 		= $this->invoke_login($cookie_jar);
				$result 		= json_decode($result,true);
				header("Content-type: text/html; charset=utf-8");
				$model_RETURNSTOCK = '{
						"FBillTypeID":{ "FNumber": "XSTHD01_SYS"},
					    "FStockOrgId":{"FNumber":"'.$websiteId.'"},
						"FDate":"'.$data.'",
						"FSaleOrgId":{"FNumber":"'.$websiteId.'"},
						"FRetcustId":{"FNumber": "2000000"},
						"SubHeadEntity": {"FExchangeRate": 1},
						"FEntity":['.$FEntity_string.']
				}';
				//echo $model_RETURNSTOCK;exit;
				$data_model 	= '{"Creator":"String","NeedUpDateFields":[],"Model":'.$model_RETURNSTOCK.'}';
				$data 			= array(
						'SAL_RETURNSTOCK',
						$data_model,
				);
				$post_content   = $this->create_postdata($data);
				$result 		= $this->invoke_save($post_content,$cookie_jar);
				$result 		= json_decode($result);
				if($result->Result->ResponseStatus->IsSuccess)
				{
					return 1;
				}
				else
				{
					return 0;
				}
			}
			else
			{
				return 0;
			}
		}
		
		/*
		 * 收款单  AR_RECEIVEBILL
		* $websiteId  站点id
		* $order      订单信息
		* $id         订单id
		* FCONTACTUNIT 客户id ; FPAYUNIT 客户id ;  FPAYORGID 组织机构 ;FRECTOTALAMOUNTFOR 应收金额 ;   现金折扣	 FSETTLEDISTAMOUNTFOR   ;  实收金额 	FREALRECAMOUNTFOR_D
		*/
		public function add_AR_RECEIVEBILL($websiteId,$order,$creditmemo){
			$cookie_jar = tempnam('./tmp','CloudSession');
			$result 	= $this->invoke_login($cookie_jar);
			$result 	= json_decode($result,true);
			header("Content-type: text/html; charset=utf-8");
			$money = $order->subtotal_incl_tax - $order->base_total_refunded;
			$model_RECEIVEBILL = '{
					  "FBillTypeID": {"FNumber": "SKDLX01_SYS"},
					  "FCONTACTUNITTYPE": "BD_Customer",
					  "FPAYUNITTYPE": "BD_Customer",
					  "FCONTACTUNIT": {"FNumber": "2000000"},
					  "FPAYUNIT": {"FNumber": "2000000"},
					  "FPAYORGID": {"FNumber": "'.$websiteId.'"},
					  "FSETTLEORGID": {"FNumber": "'.$websiteId.'"},
					  "FEXCHANGERATE": 1,
					  "FSETTLERATE": 1,
					  "FCURRENCYID": {"FNumber": "PRE001"},
					  "FBUSINESSTYPE": "1",
					  "FSETTLECUR":{"FNumber": "PRE001"},
					  "FRECEIVEBILLENTRY": [
					    {
	        			  "FPURPOSEID": {"FNumber": "SFKYT01_SYS"},
					      "FSETTLETYPEID": {"FNumber": "JSFS01_SYS"},
					      "FRECTOTALAMOUNTFOR": '.$money.',
					      "FSETTLEDISTAMOUNTFOR":'.$order->rewardpoints_discount.',
						  "FREALRECAMOUNTFOR_D": '.$money.',
						  "FCOMMENT":"'.$creditmemo['increment_id'].'",
					    }
					  ]
				}';
				
			$data_model = '{"Creator":"String","NeedUpDateFields":[],"Model":'.$model_RECEIVEBILL.'}';
			$data 		= array(
					'AR_RECEIVEBILL',
					$data_model,
			);
			$post_content = $this->create_postdata($data);
			$result 	  = $this->invoke_save($post_content,$cookie_jar);
			$result 	  = json_decode($result);
			if($result->Result->ResponseStatus->IsSuccess)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		
		/*
		 * 收款退款单  AR_REFUNDBILL
		 * FCONTACTUNIT , FRECTUNIT 客户id ; FPAYORGID ---站点id ; 订单id  FNOTE
		 *	应退金额       FREFUNDAMOUNTFOR ;
		 *	退款金额	FREFUNDAMOUNTFOR_E;
		 *	手续费	    FHANDLINGCHARGEFOR
		 *	实退金额	FREALREFUNDAMOUNTFOR_D;
		 * 
		 */
		public function add_AR_REFUNDBILL($creditmemo,$websiteId){
			$cookie_jar 	  = tempnam('./tmp','CloudSession');
			$result 		  = $this->invoke_login($cookie_jar);
			$result 		  = json_decode($result,true);
			header("Content-type: text/html; charset=utf-8");
			$model_REFUNDBILL = '{
					  "FBillTypeID": {"FNumber": "SKTKDLX01_SYS"},
					  "FCONTACTUNIT": {"FNumber": "2000000"},
					  "FRECTUNIT": {"FNumber": "2000000"},
					  "FPAYORGID": {"FNumber": "'.$websiteId.'"},
					  "FSETTLERATE": 1,
					  "FSETTLEORGID": {"FNumber": "'.$websiteId.'"},
					  "FREFUNDBILLENTRY": [
					    {
						  "FNOTE": "'.$creditmemo['order_increment_id'].'",
					      "FSETTLETYPEID": {"FNumber": "JSFS01_SYS"},
					      "FREFUNDAMOUNTFOR": '.$creditmemo['grand_total'].',
					      "FREFUNDAMOUNTFOR_E": '.$creditmemo['grand_total'].',
						  "FHANDLINGCHARGEFOR":0,
						  "FREALREFUNDAMOUNTFOR_D":'.$creditmemo['grand_total'].',
					    }
					  ]
				}';
			$data_model 	= '{"Creator":"String","NeedUpDateFields":[],"Model":'.$model_REFUNDBILL.'}';
			$data 			= array(
					'AR_REFUNDBILL',
					$data_model,
			);
			$post_content 	= $this->create_postdata($data);
			$result 		= $this->invoke_save($post_content,$cookie_jar);
			$result 		= json_decode($result);
			if($result->Result->ResponseStatus->IsSuccess)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		
}


?>